﻿using NLog;
using System;
using System.Data.SqlClient;
using GBT_Tool;

namespace DataPersistence
{
    public class Database
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        ConfigManage cm = new ConfigManage();

        /// <summary>
        /// MSSqlServer连接字符串
        /// </summary>
        /// <returns></returns>
        internal string DbConnection(string DBName)
        {
            try
            {
                SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
                scsb.DataSource = cm.Host;
                scsb.UserID = cm.UserID;
                scsb.Password = cm.Password;
                scsb.IntegratedSecurity = false;
                scsb.InitialCatalog = DBName;
                return scsb.ConnectionString;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message + "\r\n" + ex.StackTrace);
            }
        }

        /// <summary>
        /// 内部全局属性配置的数据库连接用
        /// </summary>
        /// <param name="DBName"></param>
        /// <returns></returns>
        internal SqlConnection dataConnection(string DBName)
        {
            SqlConnection sconn = new SqlConnection(DbConnection(DBName));
            return sconn;
        }

        /// <summary>
        /// 客户连接使用
        /// </summary>
        /// <param name="scomm"></param>
        /// <param name="DBName"></param>
        /// <returns></returns>
        internal SqlDataAdapter dataAdapter(SqlCommand scomm,string DBName)
        {
            scomm.Connection = dataConnection(DBName);
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = scomm;
            return sda;
        }
        
    }
}
